package com.dom.domweb.dao.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dom.common.constants.ConstantError;
import com.dom.common.enums.OrganStatEnum;
import com.dom.common.enums.UserStatEnum;
import com.dom.common.exception.ServiceException;
import com.dom.common.model.vo.basic.*;
import com.dom.common.utils.BeanUtil;
import com.dom.domweb.dao.entity.TOrganInfo;
import com.dom.domweb.dao.entity.TRoleInfo;
import com.dom.domweb.dao.entity.TSysCodeTbl;
import com.dom.domweb.dao.entity.TUserInfo;
import com.dom.domweb.dao.mapper.TOrganInfoMapper;
import com.dom.domweb.dao.service.ITOrganInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dom.domweb.dao.service.ITRoleInfoService;
import com.dom.domweb.modular.service.BasicService;
import com.dom.domweb.modular.shiro.model.ShiroUser;
import com.xiaoleilu.hutool.util.ObjectUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * B_组织机构信息表 服务实现类
 * </p>
 *
 * @author xllei
 * @since 2018-11-17
 */
@Service
public class TOrganInfoServiceImpl extends ServiceImpl<TOrganInfoMapper, TOrganInfo> implements ITOrganInfoService {
    @Autowired
    private BasicService basicService;

    @Override
    public List<TOrganInfo> findOrganList(int memCode) {
        return this.selectList(new EntityWrapper<TOrganInfo>().eq("mem_code", memCode).orderBy("id", true));
    }

    @Override
    public B049A findOrganByPager(B049R request, ShiroUser shiroUser) {
        Page<TOrganInfo> page = new Page<>();
        page.setCurrent(request.getPn());
        page.setSize(request.getPs());

        Wrapper<TOrganInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("mem_code", shiroUser.getMemCode());
        if(StringUtils.isNotEmpty(request.getOrganName())){
            wrapper.and().like("organ_name", request.getOrganName());
        }
        if(ObjectUtil.isNotNull(request.getStat())){
            wrapper.and().eq("stat", request.getStat());
        }
        wrapper.orderBy("id", true);
        page = this.selectPage(page, wrapper);

        B049A response = new B049A();
        response.setTotalNum((int) page.getTotal());

        //获取机构等级信息
        List<TSysCodeTbl> organLevels = basicService.getOrganLevelList();
        Map<String, String> roleMap = organLevels.stream().collect(Collectors.toMap(TSysCodeTbl::getSysValue, entity -> {
            return entity.getCodeDesc();
        }));

        Map<Integer, String> organNameMap = page.getRecords().stream().collect(Collectors.toMap(TOrganInfo::getId, entity -> {
            return entity.getOrganName();
        }));
        List<OrganInfo> organInfos = new ArrayList<>();
        page.getRecords().forEach(tOrganInfo -> {
            OrganInfo organInfo = new OrganInfo();
            BeanUtil.copyPropertiesIgnoreNull(tOrganInfo, organInfo);
            organInfo.setOrganCode(tOrganInfo.getId());
            organInfo.setOrganLevelDesc(roleMap.get(tOrganInfo.getOrganLevel().toString().trim()));
            organInfo.setFatherOrganCode(tOrganInfo.getFatherOrgan());
            organInfo.setFatherOrganName(organNameMap.get(tOrganInfo.getFatherOrgan()));
            organInfos.add(organInfo);
        });
        response.setOrganInfoData(organInfos);
        return response;
    }

    @Override
    public B040A addOrganInfo(B040R request) {
        TOrganInfo entity = new TOrganInfo();
        BeanUtil.copyPropertiesIgnoreNull(request, entity);
        entity.setFatherOrgan(request.getFatherOrganCode());

        boolean result = this.insert(entity);

        B040A response = new B040A();
        response.setOrganCode(entity.getId());
        return response;
    }

    @Override
    public B042A editOrganInfo(B042R request) {
        Wrapper<TOrganInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("id", request.getOrganCode())
                .last("limit 1");
        TOrganInfo organInfo = this.selectOne(wrapper);

        BeanUtil.copyPropertiesIgnoreNull(request, organInfo);

        boolean result = this.updateById(organInfo);
        B042A response = new B042A();
        response.setOrganId(organInfo.getId());
        return response;
    }

    @Override
    public B041A delOrganInfo(B041R request) {
        TOrganInfo organInfo = this.selectById(request.getOrganId());
        if (ObjectUtil.isNull(organInfo)) {
            throw new ServiceException(ConstantError.NO_ORGAN_ERROR);
        }
        organInfo.setStat(OrganStatEnum.STAT_STOP.getCode());
        organInfo.setMtime(new Date());

        boolean result = this.updateById(organInfo);
        B041A response = new B041A();
        response.setOrganId(organInfo.getId());
        return response;
    }
}
